package com.ys.malini.util;

/**
 * DateUtil.java
 * Description: 日期
 * Created By Swiftly
 * Created On Sep 20, 2008
 * Version: 1.0
 *
 **/

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class DateUtil {

	public static final DateFormat DATE_FORMAT = new SimpleDateFormat(
			"MM/dd/yyyy");
	public static final DateFormat FULL_DATE_FORMAT = new SimpleDateFormat(
			"EEE, MMM d, yyyyy hh:mm:ss aa z");
	public static final DateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd'T'HH:mm:ss.SS z");

	private static SimpleDateFormat formatter;

	/**
	 * @param baseDate
	 * @param type
	 * @param num
	 * @return
	 */
	public static Date addDate(Date baseDate, int type, int num) {
		Date lastDate = null;
		try {
			Calendar cale = Calendar.getInstance();
			cale.setTime(baseDate);
			if (type == 1) {
				cale.add(Calendar.YEAR, num);
			} else if (type == 2) {
				cale.add(Calendar.MONTH, num);
			} else if (type == 3) {
				cale.add(Calendar.DATE, num);
			}
			lastDate = cale.getTime();
			return lastDate;
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * 指定时间+时间段
	 * 
	 * @param strDate
	 * @param formatter
	 * @return
	 */
	public static String addDate(String baseDate, int type, int num) {
		String lastDate = null;
		if (baseDate == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			lastDate = formatter.format(addDate(formatter.parse(baseDate),
					type, num));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return lastDate;
	}

	/**
	 * 校验时间格式是否为HH:mm:ss
	 * 
	 * @param time
	 * @return
	 */
	public static boolean checkH2m2S2Time(String time) {
		if (StringUtils.isEmpty(time))
			return false;

		return Pattern.matches("[0-9]{2}:[0-9]{2}:[0-9]{2}", time);
	}

	/**
	 * 校验日期格式是否为yyyy-mm-dd格式
	 * 
	 * @param date
	 * @return
	 */
	public static boolean checkY4M2D2Date(String date) {
		if (StringUtils.isEmpty(date))
			return false;

		return Pattern.matches("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}", date);
	}

	public static boolean equals(Date date1, Date date2) {
		if (date1 == null && date2 == null)
			return true;
		if (date1 == null && date2 != null)
			return false;
		if (date1 != null && date2 == null)
			return false;
		return date1.equals(date2);
	}

	public static String formatDate(Date aDate, String formatStr) {
		if (aDate == null)
			return "";
		formatter = new SimpleDateFormat(formatStr);
		return formatter.format(aDate);

	}

	/***************************************************************************
	 * GET SYSTEM DATE YYYY-MM-DD HH:MM:SS
	 */
	public static String getCurrentLongDate() {
		formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(getSysDate());
	}

	/**
	 * GET SYSTEM DATE YYYY-MM-DD
	 * 
	 * @param args
	 */
	public static String getCurrentShortDate() {
		return shortDate(getSysDate());
	}

	/**
	 * GET SYSTEM TIME 12:00:00
	 * 
	 * @param args
	 */
	public static String getCurrentTime() {
		formatter = new SimpleDateFormat("HH:mm:ss");
		return formatter.format(getSysDate());
	}

	public static Date getDate(String yyyymmdd) {
		if (yyyymmdd == null)
			return null;
		int year = Integer.parseInt(yyyymmdd
				.substring(0, yyyymmdd.indexOf("-")));
		int month = Integer.parseInt(yyyymmdd.substring(
				yyyymmdd.indexOf("-") + 1, yyyymmdd.lastIndexOf("-")));
		int day = Integer.parseInt(yyyymmdd
				.substring(yyyymmdd.lastIndexOf("-") + 1));
		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, day);
		return cal.getTime();

	}

	public static Date getDate(String strDate, String formatter) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatter);
		try {
			return sdf.parse(strDate);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * @param aDate
	 * @return String
	 * @roseuid 3CECAD9D03B9
	 */
	private static final String getDateFormat(java.util.Date aDate) {
		if (aDate == null)
			return null;
		SimpleDateFormat formatter = new SimpleDateFormat("M/d/yyyy");
		return formatter.format(aDate);
	}

	public static Date getShortDate(String date) {
		Date shortDate = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			shortDate = formatter.parse(date);
		} catch (Exception e) {
			shortDate = null;
		}
		return shortDate;
	}

	/**
	 * get system date (yyyy-mm-dd)
	 */
	public static Date getSysDate() {
		return new Date(System.currentTimeMillis());
	}

	/**
	 * @param curDate
	 * @param rollUpDate
	 * @return boolean
	 * @roseuid 3CECAD76038B
	 */
	private static boolean isEndOfYear(java.util.Date curDate,
			java.util.Date rollUpDate) {
		return (curDate.compareTo(rollUpDate) >= 0);
	}

	public static String LDAPDate(Date aDate) {
		if (aDate == null)
			return "";
		return formatDate(aDate, "yyyyMMddHHmm'Z'");
	}

	public static String longDate(Date aDate) {
		if (aDate == null)
			return "";
		formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(aDate);
	}

	public static String longDateGB(Date aDate) {
		if (aDate == null)
			return "";
		formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(aDate);
	}

	public static String longDateLog(Date aDate) {
		if (aDate == null)
			return "";
		formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		return formatter.format(aDate);
	}

	public static String mailDate(Date aDate) {
		if (aDate == null)
			return "";
		formatter = new SimpleDateFormat("yyyyMMddHHmm");
		return formatter.format(aDate);
	}

/*	public static void main(String[] args) {
		System.out.println(addDate("1984-08-10", 1, 18));
	}*/

	/**
	 * @param date
	 * @return java.util.Date
	 * @roseuid 3CECAD760376
	 */
	public static Date nextDate(java.util.Date date) {
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.setTime(date);
		cal.roll(java.util.Calendar.DAY_OF_YEAR, 1);
		if (isEndOfYear(date, cal.getTime())) {
			cal.roll(Calendar.YEAR, true);
			cal.roll(java.util.Calendar.DAY_OF_YEAR, 1);
		}
		return cal.getTime();
	}

	/**
	 * @param date
	 * @return String
	 * @roseuid 3CECAD9D026F
	 */
	public static String NVL(java.util.Date date) {
		if (date == null)
			return "";
		else
			return getDateFormat(date);
	}

	public static Date parser(String strDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			return sdf.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date parser24(String strDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(strDate);
		} catch (Exception e) {
			return null;
		}
	}

	public static String shortDate(Date aDate) {
		if (aDate == null)
			return "";
		formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(aDate);
	}

	public static String shortDateGB(Date aDate) {
		if (aDate == null)
			return "";
		formatter = new SimpleDateFormat("yyyy'年'MM'月'dd'日'");
		return formatter.format(aDate);
	}

	/**
	 * @return java.util.Date
	 * @roseuid 3CECAD76033A
	 */
	public static Date tomorrow() {
		java.util.Calendar calender = java.util.Calendar.getInstance();
		calender.roll(Calendar.DAY_OF_YEAR, true);
		return calender.getTime();
	}

	/**
	 * @return java.util.Date
	 * @roseuid 3CECAD7603A0
	 */
	public static Date yesterday() {
		java.util.Calendar calender = java.util.Calendar.getInstance();
		calender.roll(Calendar.DAY_OF_YEAR, false);
		return calender.getTime();
	}
}
